package com.zy.explore.license.infrastructure.util;

import cn.hutool.core.date.DatePattern;
import com.google.common.collect.Lists;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>建类说明：日期工具类</p>
 *
 * @author 中国印
 * @date 2022年03月30日 16:01:40
 */
public class DateUtil extends cn.hutool.core.date.DateUtil {

    public static String format(Date date) {
        return format(date, DatePattern.NORM_DATETIME_FORMATTER);
    }

    public static Date getPastDate(int n) {
        return getDate(-n);
    }

    public static Date getFutureDate(int n) {
        return getDate(n);
    }

    private static Date getDate(int n) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, n);
        return c.getTime();
    }

    /**
     * 获取过去7天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static List<String> getDateList(int intervals) {
        List<String> pastDaysList = Lists.newArrayList();
        for (int i = intervals -1; i >= 0; i--) {
            pastDaysList.add(getPastDateStr(i));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     * @param pastN 过去N天
     * @return 返回日期串 yyyy-MM-dd
     */
    public static String getPastDateStr(int pastN) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - pastN);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DatePattern.NORM_DATE_PATTERN);
        return format.format(today);
    }

}
